﻿using Atlanchim.Crm.Mui.Content;
using Atlanchim.Crm.Mui.Helpers;
using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows.Controls;
using Lmondeil.Crm.EF;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Atlanchim.Crm.Mui
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : ModernWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            // Initialisation des variables globales
            SetupAppDirectories();

            // Initialisation de la base de données
            InitializeDbIfMissing();

            // Initialisation du thème
            InitializeTheme();

            // Initialisation de la langue de l'UI
            SetupUILang();
        }

        private void InitializeDbIfMissing()
        {
            using (CrmContext context = new CrmContext())
            {
                if (!context.Database.Exists())
                {
                    AdminCommandHelper.ExecuteDBReset();
                }
            }
        }

        private void SetupAppDirectories()
        {
            AppDomain.CurrentDomain.SetData("DataDirectory", LocalConstants.Folders.DatabaseFolder);
        }

        /// <summary>
        /// Initialisation du thème avec les couleurs paramétrées
        /// </summary>
        private void InitializeTheme()
        {
            using (CrmContext context = new CrmContext())
            {
                if (context.Database.Exists())
                {
                    string applicationAccentColor = CrmCrudHelper.GetParameter(ApplicationConstants.Parameters.AccentColor);
                    if (!string.IsNullOrEmpty(applicationAccentColor))
                    {
                        string[] accentComposites = applicationAccentColor.Split(';');
                        byte aComposite; byte rComposite; byte gComposite; byte bComposite;
                        byte.TryParse(accentComposites[0], out aComposite);
                        byte.TryParse(accentComposites[1], out rComposite);
                        byte.TryParse(accentComposites[2], out gComposite);
                        byte.TryParse(accentComposites[3], out bComposite);
                        AppearanceManager.Current.AccentColor = Color.FromArgb(aComposite, rComposite, gComposite, bComposite);
                    }

                    string applicationTheme = CrmCrudHelper.GetParameter(ApplicationConstants.Parameters.Theme);
                    if (!string.IsNullOrEmpty(applicationTheme))
                    {
                        AppearanceManager.Current.ThemeSource = new Uri(applicationTheme, UriKind.RelativeOrAbsolute);
                    }

                    string applicationFontSize = CrmCrudHelper.GetParameter(ApplicationConstants.Parameters.FontSize);
                    if (!string.IsNullOrEmpty(applicationFontSize))
                    {
                        AppearanceManager.Current.FontSize = applicationFontSize == ApplicationConstants.FontSize.FontLarge ? FirstFloor.ModernUI.Presentation.FontSize.Large : FirstFloor.ModernUI.Presentation.FontSize.Small;
                    }
                }
            }
        }

        /// <summary>
        /// Initialisation de la langue de l'UI avec la langue locale du poste
        /// </summary>
        private void SetupUILang()
        {
            string culture = CultureInfo.CurrentCulture.IetfLanguageTag;
            XmlLanguage language = XmlLanguage.GetLanguage(culture);

            FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(language));
            TextElement.LanguageProperty.OverrideMetadata(typeof(TextElement), new FrameworkPropertyMetadata(language));
        }
    }
}
